import type {SysRoleAdd, SysRoleData, SysRoleDetail, SysRoleParams} from "@/pages/system/role/type";
import {request} from "@@/plugin-request/request";
import download from "@/utils/download";


export const sysRolePageList = async (sysRoleParams: SysRoleParams) => {
    return request<{
        data: {
            records: SysRoleData[],
            total: number,
        }
        success: boolean
    }>('/api/sys/role/list', {
        method: 'POST',
        data: sysRoleParams
    });
}


export const sysRoleUpdate = async (sysRoleAdd: SysRoleAdd) => {
    return request<{
        data: string;
    }>('/api/sys/role/update', {
        method: 'POST',
        data: sysRoleAdd
    });
}

export const sysRoleAdd = async (sysRoleAdd: SysRoleAdd) => {
    return request<{
        data: string;
    }>('/api/sys/role/add', {
        method: 'POST',
        data: sysRoleAdd
    });
}

export const sysRoleRemove = async ( ids: (number | string)[] ) => {
    return request<{
        data: string;
    }>('/api/sys/role/remove', {
        method: 'POST',
        data: {
            ids
        }
    });
}

export const sysRoleDetail = async (id: number) => {
    return request<{
        data: SysRoleDetail;
    }>('/api/sys/role/detail', {
        method: 'POST',
        data: {
            id
        }
    });
}

export const sysRoleImport = async (formData: FormData) => {
    return request<{
        data: string;
    }>('/api/sys/role/import', {
        method: 'POST',
        data: formData,
        requestType: "form"
    });
}


const sysRoleExport = async (ids: (number | string)[]) => {
    return request<{
        data: string;
    }>('/api/sys/role/export', {
        method: 'POST',
        data: {ids},
        responseType: 'blob',
        getResponse: true,
    });
}

export const downloadSysRoleExport = async (ids: (number | string)[]) => {
    await sysRoleExport(ids).then(({data, response}) => {
        download(data, response)
    });
}


const sysRoleExportTemplate = async () => {
    return request<{
        data: string;
    }>('/api/sys/role/export/template', {
        method: 'POST',
        responseType: 'blob',
        getResponse: true,
    });
}

export const downloadSysRoleExportTemplate = async () => {
    await sysRoleExportTemplate().then(({data, response}) => {
        download(data, response)
    });
}




